package com.feiyi.app.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.R;
import com.feiyi.app.activity.ArticleDetailActivity;
import com.feiyi.app.activity.GoodsDetailActivity;
import com.feiyi.app.activity.GoodsDetailActivityNew;
import com.feiyi.app.activity.GoodsSearchActivity;
import com.feiyi.app.adapter.ClassifyRightBrandAdapter;
import com.feiyi.app.adapter.ClassifyRightCategoryAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseFragment;
import com.feiyi.app.bean.ClassifyRightBean;
import com.feiyi.app.glideutils.GlideImgManager;
import com.feiyi.app.net.HttpMethods;
import com.feiyi.app.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-15
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:分类右侧代码
 * In me the tiger sniffs the rose
 */

public class ClassifyRightFragment extends BaseFragment {

    Unbinder unbinder;

    @BindView(R.id.iv_classify_ads)
    ImageView iv_classify_ads;

    @BindView(R.id.rv_brand)
    RecyclerView rv_brand;

    @BindView(R.id.rv_category)
    RecyclerView rv_category;

    /**
     * 右侧品牌数据
     */
    private List<ClassifyRightBean.DataBean.AdsBean> adsBeanList = new ArrayList<>();
    /**
     * 右侧频道数据
     */
    private List<ClassifyRightBean.DataBean.BrandsBean> brandsBeanList = new ArrayList<>();

    private List<ClassifyRightBean.DataBean.ChildsBean> childsBeanList = new ArrayList<>();

    /**
     * 右侧品牌列表适配器
     */
    private ClassifyRightBrandAdapter rightBrandAdapter;
    /**
     * 右侧频道列表适配器
     */

    private ClassifyRightCategoryAdapter rightCategoryAdapter;

    private View view;

    private String cat_id;

    private static final String TAG = "ClassificationFragment";

    @Override
    public View getLayoutInflater(LayoutInflater inflater) {

        view = inflater.inflate(R.layout.fragment_classify_right,null);
        unbinder = ButterKnife.bind(this,view);

        return view;
    }

    @Override
    public void initView() {
        cat_id = getArguments().getString("cat_id");
        iv_classify_ads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                switch (adsBeanList.get(0).getModel()){
                    case "article":
                        bundle.putString("article_id",adsBeanList.get(0).getModel_id());
                        ActivityUtils.startActivity(bundle, ArticleDetailActivity.class);
                        break;
                    case "goods":
                        bundle.putString("goods_id",adsBeanList.get(0).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsDetailActivityNew.class);
                        break;
                    case "category":
                        bundle.putString("category_id",adsBeanList.get(0).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
                        break;
                    case "brand":
                        bundle.putString("brand_id",adsBeanList.get(0).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
                        break;

                }
            }
        });
        rv_brand.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("brand_id",brandsBeanList.get(position).getBrand_id());
                ActivityUtils.startActivity(bundle,GoodsSearchActivity.class);
            }
        });

        rv_category.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("category",childsBeanList.get(position).getCat_id());
                ActivityUtils.startActivity(bundle,GoodsSearchActivity.class);
            }
        });
    }

    @Override
    public void initDate() {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("cat_id",cat_id);
        RequestBody body = builder.build();

        HttpMethods.getInstance().info.getClassifyRight(AppConstant.CLASSIFY_RIGHT_URL,body).
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ClassifyRightBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ClassifyRightBean classifyRightBean) {

                        if (null != classifyRightBean.getData().getAds())
                            adsBeanList.addAll(classifyRightBean.getData().getAds());
                        if (null != classifyRightBean.getData().getBrands())
                            brandsBeanList.addAll(classifyRightBean.getData().getBrands());
                        if (null != classifyRightBean.getData().getChilds())
                            childsBeanList.addAll(classifyRightBean.getData().getChilds());

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString() );

                    }

                    @Override
                    public void onComplete() {
                        fillRight();

                    }
                });
    }
    private void fillRight(){
        ImageUtil.suitDisplay(iv_classify_ads,16,9,5,4);
        GlideImgManager.loadImage(activity,adsBeanList.get(0).getSrc(),iv_classify_ads);

        rightBrandAdapter.notifyDataSetChanged();

        rightCategoryAdapter.notifyDataSetChanged();
    }
    @Override
    public void initAdapter() {
        rightBrandAdapter = new ClassifyRightBrandAdapter(R.layout.item_classify_brand,brandsBeanList);
        rv_brand.setLayoutManager(new GridLayoutManager(activity,3));
        rv_brand.setAdapter(rightBrandAdapter);

        rightCategoryAdapter = new ClassifyRightCategoryAdapter(R.layout.item_classify_brand,childsBeanList);
        rv_category.setLayoutManager(new GridLayoutManager(activity,3));
        rv_category.setAdapter(rightCategoryAdapter);
    }
}
